<?php
/**
  * Captcha.php
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if (!defined('IN_CLOUD')) exit ('Access Denied!');

class Captcha
{
	//验证码的宽度
	private $width = 100;
	
	//验证码的高
	private $height = 24;
	
	//设置字体的地址
	private $font;
	
	private $fontSize = 14;
	
	//设置字体色
	private $font_color;
	
	private $background = '#FFFFFF';
	
	//设置随机生成因子
	private $strings = array('3','4','5','6','7','a','b','c','d','e','f','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y','A','B','C','D','E','F','H','J','K','M','N','P','R','S','T','U','V','W','X','Y');
	
	//生成验证码字符数
	private $codeLen = 4            ;
	
	private $img;
	
	//验证码
	private $code;
	
	public function __construct( $config = array() )
	{
		$this->width = $config['width'] >= 38 ? $config['width'] : $this->width;
		$this->height = $config['height'] >= 18 ? $config['height'] : $this->height;
		if ( $config['font'] && file_exists( LIB . 'data/font/' . $config['font'] . '.ttf' ) )
		{
			$this->font = LIB . 'data/font/' . $config['font'] . '.ttf';
		}
		else
		{
			$this->font = LIB . 'data/font/elephant.ttf';
		}
	}
	
	/**
	 * 生成验证码
	 */
	public function createCaptcha()
	{
		$this->code = self::getRandString();
		Session::set( 'captcha' , $this->code );
		$this->img = imagecreatetruecolor( $this->width , $this->height );
		
		if (!$this->font_color) 
		{
			$this->font_color = imagecolorallocate( $this->img , rand( 0 ,255 ) , rand( 0 ,255 ) , rand( 0 , 255 ) );
		} 
		else 
		{
			$this->font_color = imagecolorallocate( $this->img , hexdec( substr( $this->font_color, 1 , 2) ), hexdec( substr( $this->font_color , 3 , 2 ) ) , hexdec( substr( $this->font_color , 5 , 2 ) ) );
		}
		//设置背景色
		$this->background = imagecolorallocate( $this->img , hexdec( substr( $this->background , 1 , 2)) , hexdec( substr( $this->background , 3 , 2)) , hexdec( substr( $this->background , 5 ,2 ) ) );
		//画一个柜形，设置背景颜色。
		imagefilledrectangle($this->img , 0 , $this->height , $this->width , 0 , $this->background );
		$this->writeFont();
		$this->drawLine();
		$this->drawPoint();
		$this->outPut();
	}
	
	/**
	 * 将验证码写入图片
	 */
	private function writeFont()
	{
		for( $i = 0; $i < $this->codeLen; $i++ ) 
		{
			$charY = ( $this->height + 10 ) / 2 + rand( -1 , 1 );	
			$charX = $i * floor( $this->width / $this->codeLen ) + 2;									
			$angle = rand( -20 , 20 );	
			imagettftext( $this->img, $this->fontSize , $angle , $charX , $charY , $this->font_color , $this->font , $this->code[$i] );
		}
	}
	
	/**
	 *  画线
	 */
	private function drawLine()
	{
		for( $i=0; $i <= 5; $i++ ) 
		{							
			$linecolor = imagecolorallocate( $this->img , mt_rand( 0 , 255 ) , mt_rand( 0 , 255 ) , mt_rand( 0 , 255 ) );
			$linex = mt_rand( 1 , $this->width - 1 );
			$liney = mt_rand( 1 , $this->height - 1 );
			imageline( $this->img , $linex , $liney , $linex + mt_rand( 0 , 4 ) - 2 , $liney + mt_rand( 0 , 4 ) - 2 , $linecolor );
		}
	}
	
	/**
	 * 画点
	 */
	private function drawPoint()
	{
		for( $i=0; $i <= 32; $i++ ) 
		{							
			$pointcolor = imagecolorallocate( $this->img , mt_rand( 0 , 255 ) , mt_rand( 0 , 255 ) , mt_rand( 0 , 255 ) );
			imagesetpixel( $this->img , mt_rand( 1 , $this->width - 1 ) , mt_rand( 1 , $this->height - 1 ) , $pointcolor );
		}
	}
	
	/**
	 * 验证验证码是否正确
	 * @param string $code
	 */
	public static function checkCaptcha( $code )
	{
		$captcha = Session::get( 'captcha' );
		if ( empty( $code ) || empty( $captcha ) )
		{
			return false;
		}
		if ( strtolower( $code ) === strtolower( $captcha ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 输出验证码图像
	 */
	public function outPut()
	{
		header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
		// HTTP/1.1
		header( 'Cache-Control: private, no-store, no-cache, must-revalidate' );
		header( 'Cache-Control: post-check=0, pre-check=0, max-age=0' , false );
		// HTTP/1.0
		header( 'Pragma: no-cache' );
		@header( 'Content-type: image/png' );
		imagepng( $this->img );
		imagedestroy( $this->img );
	}
	
	/**
	 * 获取随机字符串
	 */
	public function getRandString()
	{
		$rand_str = "";
		$count = count( $this->strings );
		for ( $i = 1 ; $i <= $this->codeLen ; $i++ ) 
		{				
			$rand_str .= $this->strings[rand( 0 , $count - 1 )];
		}
		return $rand_str;
	}
}